package net.haiyue.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.haiyue.api.custom.JsonResult;
import net.haiyue.api.entity.User;
import net.haiyue.api.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Api(value = "UserController")
@RestController
@RequestMapping(value = "/user")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private UserService _service;

    @ApiOperation(value="获取所有的用户信息", notes="用于表格展示")
    @GetMapping("/index")
    public JsonResult Index(){
        return JsonResult.success(_service.All());
    }

    @ApiOperation(value="用户登录", notes="只需要用户邮箱和密码")
    @PostMapping("/login")
    public JsonResult login(@RequestBody User user){
        User info = _service.CheckUser(user.getEmail(),user.getPwd());
        if (info!=null){
            HashMap<String, String> map = new HashMap<>();
            map.put("name",info.getName());
            map.put("email",info.getEmail());
            return JsonResult.success(map);
        }
        return JsonResult.error("error");
    }
    @ApiOperation(value="添加用户", notes="特定字段不需要")
    @PostMapping("/add")
    public JsonResult add(@RequestBody User user){
        return JsonResult.success(_service.Add(user ));
    }
    @ApiOperation(value="用户下拉框")
    @GetMapping("/select")
    public  JsonResult Select(){
        return JsonResult.success(_service.selectUsers());
    }

    @ApiOperation(value="更改用户是否是admin")
    @GetMapping("/changeAdmin")
    public  JsonResult changeAdmin(@RequestParam("id") Integer id,@RequestParam("value") boolean value){
        _service.changeAdmin(id,value);
        return JsonResult.success();
    }

}
